const Joi = require('joi')
const encrypt=require('../utils/encrypt')
// 参考地址：https://juejin.cn/post/7369783680426491914?from=search-suggest
/**
 * 全局通用接口参数校验
 */
const commonValidator = (rules) => {
  return async (ctx, next) => {
    // Joi生成校验规则
    const schema = Joi.object(rules).validate(ctx.request.body)
    if (schema.error) {
      ctx.app.emit('error', schema.error, ctx)
    } else {
      await next()
    }
  }
}
/**
 * 用户密码加密
 */
const encryptPassword=async(ctx,next)=>{
  // 获取用户密码
  const {password}=ctx.request.body
  ctx.request.body.password=encrypt(password)
  await next()
}
module.exports = {
  commonValidator,
  encryptPassword
}